class Solution {
public:
    using ll = long long;
    long long maximumSubsequenceCount(string text, string pattern)
    {
        string s;
        ll p1 = 0, p2 = 0;
        for (auto e : text)
        {
            if (e == pattern[0] || e == pattern[1])
            {
                s += e;
                if (e == pattern[0]) p1++;
                else p2++;
            }
        }
        if (pattern[0] == pattern[1])
        {
            ll ret = 0;
            for (ll i = 1; i < p1; i++) ret += i;
            return ret + p1;
        }
        ll pp1 = p1, pp2 = p2;
        ll ans = 0;
        ll cnt1 = 0;
        for (auto e : s)
        {
            if (p1 && e == pattern[0])
            {
                cnt1++;
            }
            else if (e == pattern[1] && cnt1)
            {
                ans += cnt1 * p2;
                p1 -= cnt1;
                cnt1 = 0;
                p2--;
            }
            else
                p2--;
        }
        return max(ans + pp1, ans + pp2);
    }
};